package com.ruibang.glass.portal.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruibang.glass.common.constants.Constants;
import com.ruibang.glass.portal.entity.ParamEntity;
import com.ruibang.glass.portal.service.ParamService;
import com.teaming.cloud.framework2.common.model.ResultBody;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Auther: csz
 * @Date: 2018/8/23 14:21
 * @Description:
 */
@Slf4j
@Api(tags = "系统参数管理")
@RestController
@RequestMapping("/api/param")
public class ParamController {

    @Autowired
    ParamService paramService;

    @ApiOperation( value = "根据参数名称查询参数信息", response = ResultBody.class)
    @GetMapping(value = "/getByName/{name}")
    public ResultBody<ParamEntity> getByName(@PathVariable("name") String name) {
        final LambdaQueryWrapper<ParamEntity> lambda = new QueryWrapper<ParamEntity>().lambda();
        if(StringUtils.isNotBlank(name)){
            lambda.eq(ParamEntity::getName,name);
        }
        return ResultBody.success(paramService.getOne(lambda));
    }


    @GetMapping(value = "/updateByName")
    public void updateByName(@RequestParam("name") String name,@RequestParam("value") String value) {
        paramService.updateByParams(name,value);
    }

    @GetMapping(value = "/getProgressValue")
    public String getProgressValue() {
        return paramService.getProgressValue();
    }

    @GetMapping(value = "/getProgress")
    public ResultBody<Integer> getProgress() {
        String value = paramService.getByName(Constants.SYNCHRONOUS_PROGRESS);
        if(StringUtils.isNotBlank(value)){
            String[] values = value.split(Constants.SpecialSign.UNDERLINE.getName());
            if(values.length==2){
                double a = Double.parseDouble(values[0]);
                double b = Double.parseDouble(values[1]);
                return ResultBody.success((int)(a/b * 100));
            }
        }
        return ResultBody.success(0);
    }


    @GetMapping(value = "/updateProgress")
    public ResultBody updateProgress() {
        paramService.updateByParams(Constants.SYNCHRONOUS_PROGRESS,"0_100");
        return ResultBody.success();
    }
}
